home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / d3d.zip / GRPACK1.C < prev    next >
C/C++ Source or Header  |  1991-08-30  |  8KB  |  334 lines

  1. /* GRPACK1: A graphics package, using Turbo C
  2.             graphics functions, available in 
  3.             Version 1.5.
  4.             Memory model: Huge (for ALL modules involved)
  5.  
  6.             For:  Enhanced Graphics Adapter (EGA, 640 x 350)
  7.                   Color Graphics Adapter (CGA, 640 x 200)
  8.                   Hercules Graphics Adapter (HGA, 720 x 348)
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include <alloc.h>
  14. #include <conio.h>
  15. #include <string.h>
  16. #include <process.h>
  17. #include <graphics.h>
  18.  
  19. union REGS regs;
  20.  
  21. int in_textmode=1, X__max, Y__max, drawmode=1, adaptype;
  22.  
  23. FILE *fplot=NULL;
  24.  
  25. float x_max=10.0, y_max=7.0, horfact, vertfact;
  26.  
  27. static int X1, Y1, foregroundcol, backgroundcol,
  28.    g_driver=9, g_mode=2;
  29.  
  30. static long int startaddress;
  31.  
  32. void to_text(void);
  33. void textXY(int X, int Y, char *str);
  34.  
  35. int IX(float x) { return (int)(x*horfact+0.5); }
  36. int IY(float y) { return Y__max - (int)(y*vertfact+0.5); }
  37.  
  38. static void error(char *str)
  39. /* Display a message and terminate program execution */
  40. {
  41.    if(!in_textmode)
  42.       to_text();
  43.     printf("%s\n",str);
  44.    exit(1);
  45. }
  46.  
  47. void clearpage(void)    /* clear the screen */
  48. {
  49.    clearviewport();
  50. }
  51.  
  52. int iscolor(void)    /* Find out which adapter is used */
  53. {
  54. /*   detectgraph(&g_driver, &g_mode);*/
  55.     printf("found %d %d\n",g_driver, g_mode);
  56.  
  57.  
  58.    if (g_mode == MCGAHI) g_mode = MCGAMED;
  59.    if (g_mode == ATT400HI) g_mode = ATT400MED;
  60.    if (g_mode == VGAHI) g_mode = VGAHI;
  61.  
  62.     initgraph(&g_driver,&g_mode,"");     /* Set up screen size */
  63.  
  64.     return g_driver == CGA || g_driver == MCGA ||
  65.              g_driver == ATT400 ?
  66.                 1 /* 640 x 200 */ :
  67.              g_driver == EGA || g_driver == EGA64 ||
  68.              g_driver == EGAMONO || g_driver == VGA ?
  69.                 2 /* 640 x 480 */ :
  70.              g_driver == HERCMONO ? 0 /* 720 x 348 */ : -1;
  71.  
  72. }
  73.  
  74. static void setgrcon(int adaptype)
  75. /* Set graphics constants */
  76. {
  77.    startaddress = (adaptype == 2 ? 0xA0000000 : 0xB8000000);
  78.    if (adaptype == 2) {
  79.       X__max = 639; Y__max = 479;  /* VGA */
  80.    }
  81.     else if(adaptype == 1) {
  82.       X__max = 639; Y__max = 199;  /* CGA */
  83.    }
  84.    else {
  85.       X__max = 719; Y__max = 347;  /* HGA */
  86.    }
  87. }
  88.  
  89. static int grbrfun(void)
  90. /* Used by ctrlbrk, to specify what to do with a console
  91.    break */
  92. {
  93.    to_text();  /* Go to text mode first! */
  94.    return 0;
  95. }
  96.  
  97. void initgr(void)    /* Initialize graphics */
  98. {
  99.    static int again=0;
  100.    int errcode;
  101.  
  102.    if(again)
  103.       setgraphmode(g_mode);
  104.    else {
  105.       adaptype = iscolor();      /* detect driver and mode */
  106.       if(adaptype < 0)
  107.          error("Wrong display adapter");
  108.       ctrlbrk(grbrfun);          /* Set break trap, Turbo C */
  109.       initgraph(&g_driver, &g_mode, "");
  110.       errcode = graphresult();
  111.       if(errcode < 0) {
  112.          printf("Graphics error code: %d", errcode);
  113.          exit(1);
  114.       }
  115.       again = 1;
  116.       setgrcon(adaptype);
  117.    }
  118.    horfact = X__max/x_max;
  119.    vertfact = Y__max/y_max;
  120.    if(adaptype == 0){       /* HCG */
  121.       setvisualpage(1);
  122.       setactivepage(1);
  123.    }
  124.    in_textmode = 0;
  125.    foregroundcol = getcolor();
  126.    backgroundcol = getbkcolor();
  127. }
  128.  
  129. static int txtbrfun(void)
  130. {
  131.    return 0;
  132. }
  133.  
  134. void to_text(void)
  135. /* Revert to text mode */
  136. {
  137.    if(!in_textmode)
  138.       restorecrtmode();
  139.    in_textmode = 1;
  140.    ctrlbrk(txtbrfun); 
  141.    /* Restore default break interrupt handler */
  142. }
  143.  
  144. void endgr(void)
  145. /* Wait until any key is hit and revert to text mode */
  146. {
  147.    getch();
  148.    to_text();
  149. }
  150.  
  151. void dot(int X, int Y)  /* Light or darken a pixel */
  152. {
  153.    putpixel(X, Y, 
  154.       (drawmode == 0 ? (getpixel(X, Y) == foregroundcol 
  155.                      ? backgroundcol : foregroundcol) : 
  156.         (drawmode == 1 ? foregroundcol : backgroundcol)));
  157.  
  158.       /* drawmode =  1: draw positively */
  159.       /* drawmode = -1: draw negatively */
  160.       /* drawmode =  0: toggle          */
  161. }
  162.  
  163. void checkbreak(void)
  164. {
  165.    char ch;
  166.  
  167.    if(kbhit()) {
  168.       ch = getch();
  169.       kbhit();
  170.         ungetch(ch);
  171.     }
  172. }
  173.  
  174. void draw_line(int X1, int Y1, int X2, int Y2)
  175. /* Draw the line segment from (X1,Y1) to (X2,Y2);
  176.    X1, Y1, X2, Y2 are pixel coordinates         */
  177. {
  178.     if(drawmode == 1)
  179.       line(X1, Y1, X2, Y2);
  180.  
  181. /* 'drawmode' values 0 and -1 are not implemented for
  182.    this function.  See also dot()
  183. */
  184.  
  185. }
  186.  
  187. static void fatal(void)
  188. /* Draw a diagonal, then wait until a key is pressed, and 
  189.    finally revert to text mode
  190. */
  191. {
  192.    draw_line(0,Y__max, X__max, 0);
  193.    endgr();
  194. }
  195.  
  196. static void check(int X, int Y)
  197. /* If point (X,Y) lies outside the screen boundaries, then
  198.    call fatal, display the incorrect coordinates, and stop
  199. */
  200. {
  201.    if(X < 0 || X > X__max || Y < 0 || Y > Y__max) {
  202.       fatal();
  203.       printf("Point outside screen (X and Y are pixel coordinates):\n");
  204.       printf("X = %d        Y = %d\n", X, Y);
  205.       printf("x = %10.3f    y = %10.3f\n",X/horfact,(Y__max-Y)/vertfact);
  206.       printf("X__max = %d Y__max = %d x_max = %f y_max = %f\n",
  207.               X__max,     Y__max,     x_max,     y_max);
  208.       printf("horfact = %f  vertfact = %f\n", horfact, vertfact);
  209.       exit(1);
  210.    }
  211. }
  212.  
  213. void move(float x, float y)
  214. /* Move the current point to (X, Y)
  215.    X and Y are screen coordinates  */
  216. {
  217.    int XX, YY;
  218.  
  219.    XX = IX(x);
  220.    YY = IY(y);
  221.    check(XX, YY);
  222.    if(fplot != NULL && (XX != X1 || YY != Y1))
  223.       fprintf(fplot, "%6.3f %6.3f 0\n", x, y);
  224.    X1 = XX;
  225.    Y1 = YY;
  226. }
  227.  
  228. void draw(float x, float y)
  229. /* Draw a line segment from the current point to (x, y) */
  230. {
  231.    int X2, Y2;
  232.    X2 = IX(x);
  233.    Y2 = IY(y);
  234.    check(X2, Y2);
  235.    draw_line(X1, Y1, X2, Y2);
  236.    if(fplot != NULL)
  237.       fprintf(fplot, "%6.3f %6.3f 1\n", x, y);
  238.    X1 = X2;
  239.    Y1 = Y2;
  240. }
  241.  
  242. int pixlit(int X, int Y)
  243. /* Inquire if pixel (X, Y) is lit */
  244. {
  245.    return(getpixel(X, Y) == foregroundcol);
  246. }
  247.  
  248. void printgr(int Xlo, int Xhi, int Ylo, int Yhi)
  249. /* Print contents of rectangle on matrix printer */
  250. {
  251. }
  252.  
  253. void setprdim(void)
  254. /* This function sets x_max and y_max such that graphics
  255.    results will eventually be printed with correct
  256.    dimensions, both horizontally and vertically */
  257. {
  258.    extern float x_max, y_max;
  259.    extern int X__max, Y__max;
  260.    setgrcon(iscolor());
  261.    x_max = (X__max + 1)/120.0;
  262.    y_max = (Y__max + 1) /72.0;
  263. }
  264.  
  265. void textXY(int X, int Y, char *str)
  266. /* Display string str in graphics mode, starting at (X, Y) */
  267. {
  268.    int height, width;
  269.    height = textheight(str);
  270.    width = textwidth(str);
  271.    setviewport(X, Y, X+width, Y+height, 0);
  272.    clearviewport();        /* To erase old text, if any */
  273.    outtext(str);
  274.    setviewport(0, 0, X__max, Y__max, 0);
  275. }
  276.  
  277. void text(char *str)
  278. /* Display string str, starting at the current point (X1, Y1). */
  279. {
  280.    textXY(X1, Y1, str);
  281.    X1 += strlen(str)*8;
  282.    check(X1, Y1);
  283. }
  284.  
  285. void imove(int X, int Y)
  286. /* Let (X, Y) be the new current point */
  287. {
  288.    check(X, Y);
  289.    X1 = X;
  290.    Y1 = Y;
  291. }
  292.  
  293. void idraw(int X, int Y)
  294. /* Draw a line segment from the current poitnt to (X, Y) */
  295. {
  296.    check(X, Y);
  297.    draw_line(X1, Y1, X, Y);
  298.    X1 = X;
  299.    Y1 = Y;
  300. }
  301.  
  302. void wrscr(int row, int col, char *str)  /* in textmode */
  303. /* Writes characters into video display memory while in text mode 
  304.    row = 0, 1, 2, ... ; col = 0, 1, 2, ..., 79                    */
  305. {
  306.    gotoxy(col+1, row+1);
  307.    cputs(str);
  308. }
  309.  
  310. void clearscr(void)
  311. /* Clear text mode screen (for graphics, use clearpage) */
  312. {
  313.    clrscr();
  314. }
  315.  
  316. void settxtcursor(int row, int col)
  317. {
  318.     gotoxy(col+1, row+1);
  319. }
  320.  
  321. void far *far _graphgetmem(unsigned size)
  322. {
  323.    char *p;
  324.    p = farmalloc((long)size);
  325.    if(p == NULL)
  326.       error("Mem. space in _graphgetmem");
  327.    return p;
  328. }
  329.  
  330. void far _graphfreemem(void far *ptr, unsigned size)
  331. {
  332.     farfree(ptr);
  333. }
  334.